package auction.model;

import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;

import auction.chapter16.BusinessException;

public class Item{
	private Long id;
	private String title;
	private String title2;
	private User seller;
	
	private Set<Bid> bids=new HashSet<Bid>();
	private Set<Image> images=new HashSet<Image>();
	
	public Item(){
	}
	
	public boolean equals(Object obj){
		if(obj instanceof Item){
			Item other=(Item)obj;
			boolean a= this.getTitle().equalsIgnoreCase(other.getTitle()) && this.getTitle2().equalsIgnoreCase(other.getTitle2());
			System.out.println("xxxxx "+a);
			return a;
		}else{
			System.out.println("????????");
			return false;
		}
	}
	
	public int hashCode(){
		return this.getTitle().hashCode() + this.getTitle2().hashCode();
	}
	
	public Item(String t){
		title=t;
	}
	
	public Long getId(){
		return id;
	}
	public void setId(Long i){
		id=i;
	}
	
	public void setBids(Set<Bid> i){
		bids=i;
	}
	public Set<Bid> getBids(){
		return bids;
	}
	/* GOOD!!! */
	public void addBid(Bid bid){
		//System.out.println("GOOD!!!");
		bid.setItem(this);
		bids.add(bid);
	}
	
	public void setTitle(String i){
		title=i;
	}
	public String getTitle(){
		return title;
	}

	public Set<Image> getImages() {
		return images;
	}

	public void setImages(Set<Image> images) {
		this.images = images;
	}

	public User getSeller() {
		return seller;
	}

	public void setSeller(User seller) {
		this.seller = seller;
	}

	public String getTitle2() {
		return title2;
	}

	public void setTitle2(String title2) {
		this.title2 = title2;
	}
	
	/**
	 * Chapter 16: design smart domain models
	 * @param bidder
	 * @param bidAmount
	 * @param currentMaxBid
	 * @param currentMinBid
	 * @return
	 */
	public Bid placeBid(User bidder,BigDecimal bidAmount,Bid currentMaxBid,Bid currentMinBid) throws BusinessException{
		if(currentMaxBid != null && currentMaxBid.getAmount().compareTo(bidAmount) > 0){
			throw new BusinessException("Bid too low.");
		}
		
		Bid newBid=new Bid();
		newBid.setAmount(bidAmount);
		newBid.setItem(this);
		newBid.setBidder(bidder);
		this.addBid(newBid);
		
		return newBid;
	}
}